/**   
* @Title: JobServiceImpl.java
* @Package org.xaoyaoyao.cloud.job.serivce.impl
* @Description: TODO
* @author xaoyaoyao
* @date 2018年9月7日 下午1:50:19
* @version V1.0   
*/
package org.xaoyaoyao.cloud.job.serivce.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.xaoyaoyao.cloud.job.entity.QuartzEntity;
import org.xaoyaoyao.cloud.job.serivce.JobService;
import org.xaoyaoyao.cloud.jpa.dynamic.DynamicQuery;

/**
 * @ClassName: JobServiceImpl
 * @Description: TODO
 * @author xaoyaoyao
 * @date 2018年9月7日 下午1:50:19
 * 
 */
@Service("jobService")
public class JobServiceImpl implements JobService {

	@Autowired
	private DynamicQuery dynamicQuery;

	public List<QuartzEntity> listQuartzEntity(QuartzEntity quartz, Integer pageNo, Integer pageSize) {
		StringBuffer nativeSql = new StringBuffer();
		nativeSql.append("SELECT job.job_name as jobName, job.job_group as jobGroup, job.description as description, job.job_class_name as jobClassName,");
		nativeSql.append("cron.cron_expression as cronExpression, tri.trigger_name as triggerName, tri.trigger_state as triggerState,");
		nativeSql.append("job.job_name as oldJobName, job.job_group as oldJobGroup ");
		nativeSql.append("FROM qrtz_job_details AS job LEFT JOIN qrtz_triggers AS tri ON job.job_name = tri.job_name ");
		nativeSql.append("LEFT JOIN qrtz_cron_triggers AS cron ON cron.trigger_name = tri.trigger_name ");
		nativeSql.append("WHERE tri.trigger_type = 'CRON'");
		Object[] params = new Object[] {};
		if (!StringUtils.isEmpty(quartz.getJobName())) {// 加入JobName搜索其他条件自行实现
			nativeSql.append(" AND job.job_name = ?");
			params = new Object[] { quartz.getJobName() };
		}
		nativeSql.append(" ORDER BY job.job_class_name");
		return dynamicQuery.nativeQueryListModel(QuartzEntity.class, nativeSql.toString(), params);
	}

	public Long listQuartzEntity(QuartzEntity quartz) {
		StringBuffer nativeSql = new StringBuffer();
		nativeSql.append("SELECT COUNT(*)");
		nativeSql.append("FROM qrtz_job_details AS job LEFT JOIN qrtz_triggers AS tri ON job.job_name = tri.job_name ");
		nativeSql.append("LEFT JOIN qrtz_cron_triggers AS cron ON cron.trigger_name = tri.trigger_name ");
		nativeSql.append("WHERE tri.trigger_type = 'CRON'");
		return dynamicQuery.nativeQueryCount(nativeSql.toString(), new Object[] {});
	}
}
